package com.shockweb.common.virtualconn.factory;

import com.shockweb.common.virtualconn.connection.VirtualConnection;

/**
 * 连接工厂包装类
 * @author 彭明华
 * 2017-8-4 创建
 */
public class ConnentionFactoryWapper<T extends VirtualConnection> {
	
	/**
	 * 连接工厂
	 */
	private ConnentionFactory<T> connentionFactory;
	

	/**
	 * 构造方法
	 * @param connentionFactory
	 */
	public ConnentionFactoryWapper(ConnentionFactory<T> connentionFactory){
		this.connentionFactory = connentionFactory;
	}

	
	/**
	 * 返回当前工厂
	 * @return
	 */
	public  ConnentionFactory<T> getConnentionFactory(){
		return connentionFactory;
	}
	/**
	 * 检查连接工厂是否失效
	 */
	public void check(long tryTime){
		if(isAble()){
			if(connentionFactory.check()){
				unAbleTime = -1;
			}else{
				unAbleTime = System.currentTimeMillis();
			}
		}else{
			if(System.currentTimeMillis()-unAbleTime>tryTime){
				if(connentionFactory.check()){
					unAbleTime = -1;
				}else{
					unAbleTime = System.currentTimeMillis();
				}
			}
		}
	}
	
	/**
	 * 不可用时间
	 */
	public long unAbleTime = -1;
	/**
	 * 当前工厂是否可用
	 * @return
	 */
	public boolean isAble(){
		return unAbleTime==-1;
	}
	/**
	 * wapper的详细信息
	 * @return
	 */
	public String toString(){
		return "wapper:" + isAble() + ",connentionFactory:" + connentionFactory.toString();
	}
	
}
